package com.xiaoka.ins.corp.api.gpic.dto.config;

import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaoka.ins.corp.api.utils.BaseUtils;

public class Regions {

	static {
		try {
			String json = FileUtils.readFileToString(BaseUtils.getFileByName("config/company/gpic.json")).replaceAll("\\s*", "");
			regions = readJson(json,List.class, Region.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<Region> regions;

	public static <T> T readJson(String jsonStr, Class<?> collectionClass,
			Class<?>... elementClasses) {
		ObjectMapper mapper = new ObjectMapper();
		JavaType javaType = mapper.getTypeFactory().constructParametricType(
				collectionClass, elementClasses);
		try {
			return mapper.readValue(jsonStr, javaType);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getCityCode(String cityId) {
		for (Region region : Regions.regions) {
			if (cityId.equals(region.getCityId())) {
				return region.getAreaCode();
			}
		}
		return null;
	}

	public static List<Region> getRegions() {
		return regions;
	}

	public static void setRegions(List<Region> regions) {
		Regions.regions = regions;
	}

	public static class Region {

		/**
		 * 机构简写
		 */
		public String shortName;

		/**
		 * 典典机构ID
		 */
		public String cityId;

		/**
		 * 机构名称
		 */
		public String cityName;

		/**
		 * 车险公司机构ID
		 */
		public String areaCode;

		public String getShortName() {
			return shortName;
		}

		public void setShortName(String shortName) {
			this.shortName = shortName;
		}

		public String getCityId() {
			return cityId;
		}

		public void setCityId(String cityId) {
			this.cityId = cityId;
		}

		public String getCityName() {
			return cityName;
		}

		public void setCityName(String cityName) {
			this.cityName = cityName;
		}

		public String getAreaCode() {
			return areaCode;
		}

		public void setAreaCode(String areaCode) {
			this.areaCode = areaCode;
		}

	}

}
